package com.woniu.uums.service;

import com.woniu.uums.feign.LoginFeign;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.thymeleaf.util.StringUtils;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * @program: finance021
 * @description:
 * @create: 2024-11-06 17:38
 **/
@Service
public class UserService {
    @Autowired
    private LoginFeign loginFeign;
    @Autowired
    private HttpServletRequest request;
    @Autowired
    private HttpServletResponse response;
    /**
     * 登录
     *
     * @param username 用户名
     * @param password 密码
     * @return
     */
    public String login(String username, String password,String returnUrl) {
        //继续传url
        request.setAttribute("returnUrl", returnUrl);
        // 登录
        String jwt = loginFeign.login(username, password);
        if (StringUtils.isEmpty(jwt)){
            //返回登录
            //给用户提示
            request.setAttribute("nologin","用户名或密码错误");
            return "login";
        }

        //把jwt放到cookie
        Cookie cookie = new Cookie("jwt",jwt);
        cookie.setDomain("localhost");//域名
        cookie.setPath("/");
        response.addCookie(cookie);

        //返回首页
        return "index";
    }
}
